package com.wclass.mapper;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.wclass.entity.CourseWare;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课件 Mapper 接口
 * </p>
 *
 * @author 张浩高
 * @since 2018-12-20
 */
public interface CourseWareMapper extends BaseMapper<CourseWare> {

    /**
     * 课程统计课件分页
     * @param page
     * @param userId
     * @return
     */
    @Select("select (select COUNT(*) from course_ware ware where info.id = ware.course_info_id ) as num,case when SUM(ware.attachment_size) is null then 0 " +
            "else SUM(ware.attachment_size) end as size,MAX(ware.crt_time) as time,info.name as courseName,info.id as courseInfoId " +
            "from course_info info left join course_ware ware on info.id = ware.course_info_id " +
            "where info.audit_state=1 and info.user_id = #{userId} and info.state = 0 " +
            "group by info.id order by info.crt_time desc")
    List<Map<String, Object>> selectCourseWareCount(Pagination page, @Param("userId") Long userId);

    @Select("select (select COUNT(*) from course_ware ware where info.id = ware.course_info_id ) as num,case when SUM(ware.attachment_size) is null then 0 " +
            "else SUM(ware.attachment_size) end as size,MAX(ware.crt_time) as time,info.name as courseName,info.id as courseInfoId " +
            "from course_info info left join course_ware ware on info.id = ware.course_info_id " +
            "where info.audit_state=1 and info.user_id = #{userId} and info.state = 0 and info.id = #{courseInfoId} " +
            "group by info.id order by info.crt_time desc")
    List<Map<String, Object>> selectCourseWareCountByCourseId(@Param("userId") Long userId,@Param("courseInfoId")  Long courseInfoId);

}
